package com.hbpu.encap.generic;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Homework01 {
    public static void main(String[] args) {
        DAO<User> userDAO = new DAO<>();
        userDAO.save("0100", new User("mike", 11, 20));
        userDAO.save("0110", new User("jack", 22, 30));
        userDAO.save("0111", new User("bob", 33, 40));

        System.out.println(userDAO.get("0100"));
        System.out.println(userDAO.list());

        userDAO.update("0111", new User("bob", 44, 50));
        System.out.println(userDAO.get("0111"));

        System.out.println(userDAO.list());

        userDAO.delete("0110");
        System.out.println(userDAO.list());
    }
}

class DAO<T> {
    private Map<String, T> map;
    public DAO() {
        map = new HashMap<String, T>();
    }

    public Map<String, T> getMap() {
        return map;
    }

    public void setMap(Map<String, T> map) {
        this.map = map;
    }

    public void save(String id, T entity) {
        map.put(id, entity);
    }
    public T get(String id) {
        return map.get(id);
    }
    public void update(String id, T entity) {
        map.put(id, entity);
    }

    public List<T> list () {
        return new ArrayList<T>(map.values());
    }

    public void delete(String id) {
        map.remove(id);
    }
}

class User {
    private String name;
    private int id;
    private int age;

    public User(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}